# nas下载更新包到c:poi文件夹
import glob
import os
import zipfile

import pyautogui
import requests


def update_tools():
    os.system('taskkill /F /IM hngjx.exe')
    # url是更新包的nas链接
    url1 = 'http://192.168.65.50/hngjx.zip'
    lj = 'c:\\poi\\' + url1.split('/')[-1]
    # 指定要删除文件的目录
    directory = r'C:\Users\hn\Desktop'

    # 使用glob模块匹配所有文件名中包含"example"的文件
    file_pattern = os.path.join(directory, '*海能工具箱*')
    files = glob.glob(file_pattern)

    # 遍历匹配到的文件并删除
    for file in files:
        os.remove(file)
        print(f"Deleted file: {file}")

    r = requests.get(url=url1)
    if r.status_code == 404:
        print('无更新包')
    else:
        open(lj, 'wb').write(r.content)
        if zipfile.is_zipfile(lj):
            with zipfile.ZipFile(lj) as zfp:
                zfp.extractall('c:\\poi')
            print(os.path.isdir(r'C:\Users\hn\Desktop'))
            if os.path.isdir(r'C:\Users\hn\Desktop'):
                os.system('mklink ' + r'C:\Users\hn\Desktop\海能工具箱v3.exe' + ' ' + r'C:\poi\hngjx\hngjx.exe')
                pyautogui.alert(text='程序更新完成，请重新打开工具箱', title='成功')
            else:
                pyautogui.alert(text='快捷方式创建失败', title='失败')


update_tools()
